/**
 * 
 */
package controlleur.form;

import java.rmi.RemoteException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import annuaire.beans.Promotion;
import annuaire.exceptions.BusinessException;
import annuaire.rmi.remotedObject.IRemotedAnnuaire;
import annuaire.rmi.remotedObject.IRemotedUser;
import form.bean.PromotionForm;

/**
 * Contrôleur responsable de la suppression par l'administrateur d'une promotion
 * en particulier
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class DeletePromoFormControlleur extends SimpleFormController {
	/** Logger pour cette classe ainsi que pour ses sous classes */
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Reference vers l'objet distant Annuaire
	 */
	private IRemotedAnnuaire remotedAnnuaire;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object commandAjout,
			BindException errors) throws Exception {

		// On recupere l'id de la promo qui a ete choisie
		Integer idPromoForm = Integer.parseInt(request.getParameter("id"));
		// On va chercher la promo correspondante
		Promotion pToDelete = remotedAnnuaire.findPromoById(idPromoForm, true);

		// Recuperation de l'utilisateur en session
		IRemotedUser user = (IRemotedUser) request.getSession().getAttribute(
				"user");

		// Suppression de la promo (avec en cascade les etudiants qui la
		// composent)
		user.getAdminService().deletePromo(pToDelete);

		// On reste sur la meme page pour confirmer
		ModelAndView mav = showForm(request, response, errors);
		mav.addObject("successDeletePromo", "Promotion supprimée.");
		return mav;
		// return new ModelAndView("redirect", "model", myModel) ;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.springframework.web.servlet.mvc.AbstractFormController#
	 * handleRequestInternal(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String errorGet = null;

		// si id n'est pas fourni
		String idPromoForm = request.getParameter("id");
		if (idPromoForm == null || idPromoForm.equals("")) {
			errorGet = "erreur id absent";
		} else {
			// si l'id n'est pas un nombre
			try {
				int id = Integer.parseInt(idPromoForm);

				// si l'id n'existe pas
				Promotion p = remotedAnnuaire.findPromoById(id, false);
				if (p == null) {
					errorGet = "erreur id inexistant";
				}

			} catch (NumberFormatException e) {
				errorGet = "l'id n'est pas un entier valide";
			}
		}

		if (errorGet != null) {
			logger.info("un petit malin a tenté de jouer avec l'url");
			ModelAndView mav = new ModelAndView(getFormView());
			mav.addObject("errorGet", errorGet);
			return mav;
		} else {
			logger.info("pas d'erreur d'url");
			return super.handleRequestInternal(request, response);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {
		PromotionForm promoForm = new PromotionForm();
		Integer idPromoForm = Integer.parseInt(request.getParameter("id"));
		try {
			Promotion p = remotedAnnuaire.findPromoById(idPromoForm, false);

			promoForm.setPromoYear(p.getPromoYear());
			promoForm.setTitle(p.getTitle());
			promoForm.setDescription(p.getDescription());

		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (BusinessException e) {
			e.printStackTrace();
		}

		return promoForm;
	}

	/**
	 * @param remotedAnnuaire
	 */
	public void setRemotedAnnuaire(IRemotedAnnuaire remotedAnnuaire) {
		this.remotedAnnuaire = remotedAnnuaire;
	}

	/**
	 * @return
	 */
	public IRemotedAnnuaire getRemotedAnnuaire() {
		return remotedAnnuaire;
	}
}
